<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加老师</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>添加老师</h1>
<form id="addTeacherForm">
    <div>
        <label for="teacherName">姓名：</label>
        <input type="text" id="teacherName" name="teacherName">
    </div>
    <div>
        <label for="gender">性别：</label>
        <select id="gender" name="gender">
            <option value="1">男</option>
            <option value="0">女</option>
        </select>
    </div>
    <div>
        <label for="phone">手机：</label>
        <input type="text" id="phone" name="phone">
    </div>
    <div>
        <label for="address">地址：</label>
        <input type="text" id="address" name="address">
    </div>
    <div>
        <label for="email">邮箱：</label>
        <input type="text" id="email" name="email">
    </div>
    <div>
        <input type="button" id="submitBtn" value="添加">
    </div>
</form>

<script>
    $(document).ready(function() {
        $("#submitBtn").click(function() {
            // var formData = $("#addTeacherForm").serialize(); // 将表单数据序列化为字符串

            var teacherName = $("#teacherName").val();
            var gender = $("#gender").val();
            var phone = $("#phone").val();
            var address = $("#address").val();
            var email = $("#email").val();

            if (teacherName == "") {
                alert("姓名不能为空！");
                return false;
            }
            if (phone == "") {
                alert("手机不能为空！");
                return false;
            }
            if (address == "") {
                alert("地址不能为空！");
                return false;
            }
            if (email == "") {
                alert("邮箱不能为空！");
                return false;
            }
            var formData = {
                "teacherName": teacherName,
                "gender": gender,
                "phone": phone,
                "address": address,
                "email": email
            };
            console.log(formData);

            $.ajax({
                type: "POST",
                headers: {
                    "Content-Type": "application/json;charset=UTF-8"
                },
                url: "http://localhost:8081/studen_manage_war_exploded/teachers/addTeacher", // 处理表单数据的JSP页面
                data:  JSON.stringify(formData),
                success: function(response) {
                    console.log(response);
                    // 处理服务器返回的响应
                    alert("教师信息已成功添加！");
                },
                error: function(xhr, status, error) {
                    // 处理请求错误
                    alert("添加教师信息失败：" + error);
                }
            });
        });
    });
</script>
</body>
</html>
